class EventBus {
    constructor() {// 存放事件对象和订阅者的名称
        this.events = {};
    }

    on(eventName, callback) {// 注册事件
        if (!this.events[eventName]) {
            this.events[eventName] = [];
            // this.events[eventName] = new Set();
        }
        this.events[eventName].push(callback);
        // this.events[eventName].add(callback);
    }

    off(eventName, callback) {// 删除事件
        if (this.events[eventName]) {
            console.log("删除前：" + this.events[eventName]);

            const index = this.events[eventName].indexOf(callback)
            this.events[eventName].splice(index, 1);
            // this.events[eventName].delete(callback);
            console.log("删除后：" + this.events[eventName]);
        }
        return;
    }

    emit(eventName, ...args) {// 触发提交事件
        this.events[eventName]
            && this.events[eventName].forEach(callback => {
                console.log("触发");
                callback(...args);
            });
    }

}

var fun = function n(arg) {
    console.log('hello,', arg);
}


let ev = new EventBus()
ev.on('myevent', fun)
ev.emit('myevent', 'jack')
ev.off('myevent', fun)
ev.emit('myevent', 'jack')